#include "hplcsparklinkmakepacket.h"
#include "convert.h"
#include "hplcsparklinkpacketformat.h"
#include <QDebug>

namespace hplc
{
namespace sparklink
{

HplcSparkLinkMakePacket::HplcSparkLinkMakePacket(HplcSparkLinkAutoSamplerProtocolData &data)
    : m_d(&data)
{

}

void HplcSparkLinkMakePacket::makeAck()
{
    m_d->m_txBuffer.clear();
    m_d->m_txBuffer.append((char)SPARKLINK_PROTOCOL_ACK);
}

void HplcSparkLinkMakePacket::makeNAck()
{
    m_d->m_txBuffer.clear();
    m_d->m_txBuffer.append((char)SPARKLINK_PROTOCOL_NACK);
}

void HplcSparkLinkMakePacket::makeNAck0()
{
    m_d->m_txBuffer.clear();
    m_d->m_txBuffer.append((char)SPARKLINK_PROTOCOL_NACK0);
}

void HplcSparkLinkMakePacket::add_10(quint8 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 1);
    m_d->m_txBuffer.append((char)buffer[0]);
}

void HplcSparkLinkMakePacket::add_100(quint8 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 2);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
}

void HplcSparkLinkMakePacket::add_1000(quint16 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 3);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
    m_d->m_txBuffer.append((char)buffer[2]);
}

void HplcSparkLinkMakePacket::add_10000(quint16 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 4);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
    m_d->m_txBuffer.append((char)buffer[2]);
    m_d->m_txBuffer.append((char)buffer[3]);
}

void HplcSparkLinkMakePacket::add_100000(quint32 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 5);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
    m_d->m_txBuffer.append((char)buffer[2]);
    m_d->m_txBuffer.append((char)buffer[3]);
    m_d->m_txBuffer.append((char)buffer[4]);
}

void HplcSparkLinkMakePacket::add_1000000(quint32 value)
{
    uint8_t buffer[6];
    Convert::Int2BCD(buffer, value, 5);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
    m_d->m_txBuffer.append((char)buffer[2]);
    m_d->m_txBuffer.append((char)buffer[3]);
    m_d->m_txBuffer.append((char)buffer[4]);
    m_d->m_txBuffer.append((char)buffer[5]);
}

void HplcSparkLinkMakePacket::addGlobalId()
{
    add_100(0);
}

void HplcSparkLinkMakePacket::addId()
{
    add_100(m_d->m_id);
}

void HplcSparkLinkMakePacket::addAi(quint8 ai)
{
    uint8_t buffer[3];
    Convert::Hex2Ascii(buffer, &ai, 1);
    m_d->m_txBuffer.append((char)buffer[0]);
    m_d->m_txBuffer.append((char)buffer[1]);
}

void HplcSparkLinkMakePacket::addPfc(quint16 pfc)
{
    add_10000(pfc);
}

void HplcSparkLinkMakePacket::addSpace(quint8 cnt)
{
    for (int i = 0; i < cnt; i++)
        m_d->m_txBuffer.append((char)0x20);
}

void HplcSparkLinkMakePacket::addZero(quint8 cnt)
{
    for (int i = 0; i < cnt; i++)
        m_d->m_txBuffer.append((char)0x00);
}

void HplcSparkLinkMakePacket::addHeadAndTail()
{
    m_d->m_txBuffer.prepend(SPARKLINK_PROTOCOL_STX);
    m_d->m_txBuffer.append(SPARKLINK_PROTOCOL_ETX);
}

void HplcSparkLinkMakePacket::preMakePacket()
{
    m_d->m_txBuffer.clear();
}

void HplcSparkLinkMakePacket::postMakePacket()
{
    addHeadAndTail();
}

} // sparklink
} // hplc
